home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / dns / rovers / Ctools / ctools.c next >
Encoding:
C/C++ Source or Header  |  1991-04-24  |  19.4 KB  |  920 lines

  1. /************************************************************/
  2. /*                         CTOOLS.C                         */
  3. /* Assortment of "C" tools collected from all over!        */
  4. /************************************************************/
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include "ctools.h"
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <signal.h>
  11.  
  12. #define EQ ==
  13. #define OR ||
  14. #define AND &&
  15. #define TRUE 1
  16. #define FALSE 0
  17. typedef int BOOLEAN;
  18.  
  19. #define MAX_CLMS 256
  20.  
  21. /* TABLES FOR COMPUTING DAY OF WEEK FROM 1700-2099 */
  22.  
  23. /* Computing the day of the week can be done in a much simpler  */
  24. /* and faster way for a practically unlimited number of years.  */
  25. /* If upgrades are made, I will probably use this other method. */
  26. /* There are also faster methods for calculating between dates. */
  27.  
  28. static int dayarray[14][12]={{4,7,7,3,5,1,3,6,2,4,7,2},  /* A */
  29.                              {5,1,1,4,6,2,4,7,3,5,1,3},  /* B */
  30.                              {6,2,3,6,1,4,6,2,5,7,3,5},  /* C */
  31.                              {1,4,4,7,2,5,7,3,6,1,4,6},  /* D */
  32.                              {2,5,5,1,3,6,1,4,7,2,5,7},  /* E */
  33.                              {3,6,6,2,4,7,2,5,1,3,6,1},  /* F */
  34.                              {4,7,1,4,6,2,4,7,3,5,1,3},  /* G */
  35.                              {6,2,2,5,7,3,5,1,4,6,2,4},  /* H */
  36.                              {7,3,3,6,1,4,6,2,5,7,3,5},  /* I */
  37.                              {2,5,6,2,4,7,2,5,1,3,6,1},  /* J */
  38.                              {7,3,4,7,2,5,7,3,6,1,4,6},  /* K */
  39.                              {1,4,5,1,3,6,1,4,7,2,5,7},  /* L */
  40.                              {3,6,7,3,5,1,3,6,2,4,7,2},  /* M */
  41.                              {5,1,2,5,7,3,5,1,4,6,2,4}}; /* N */
  42.  
  43. static int find_year[14][12]={
  44.  
  45. {1710,1721,1727,1806,1817,1823,1902,1913,1919,2014,2025,2031}, /* A */
  46. {1705,1711,1722,1801,1807,1818,1903,1914,1925,2015,2026,2037}, /* B */
  47. {1712,1808,1904,2016,-999,-999,-999,-999,-999,-999,-999,-999}, /* C */
  48. {1702,1713,1719,1809,1815,1826,1905,1911,1922,2017,2023,2034}, /* D */
  49. {1703,1714,1725,1810,1821,1827,1906,1917,1923,2018,2029,2035}, /* E */
  50. {1709,1715,1726,1805,1811,1822,1901,1907,1918,2013,2019,2030}, /* F */
  51. {1716,1812,1908,2020,-999,-999,-999,-999,-999,-999,-999,-999}, /* G */
  52. {1706,1717,1723,1802,1813,1819,1909,1915,1926,2021,2027,2038}, /* H */
  53. {1701,1707,1718,1803,1814,1825,1910,1921,1927,2022,2033,2039}, /* I */
  54. {1720,1816,1912,2024,-999,-999,-999,-999,-999,-999,-999,-999}, /* J */
  55. {1724,1820,1916,2028,-999,-999,-999,-999,-999,-999,-999,-999}, /* K */
  56. {1708,1804,1928,2040,-999,-999,-999,-999,-999,-999,-999,-999}, /* L */
  57. {1704,1828,1924,2036,-999,-999,-999,-999,-999,-999,-999,-999}, /* M */
  58. {1728,1824,1920,2032,-999,-999,-999,-999,-999,-999,-999,-999}};/* N */
  59.  
  60. extern char *malloc();
  61.  
  62. char ct_buff[1000];
  63.  
  64. char *right(string,n)
  65. char string[];
  66. int n;
  67. {
  68.    register int l,i;
  69.    char result[MAX_CLMS];
  70.  
  71.    i=0;
  72.  
  73.    for (l=strlen(string)-n; l<strlen(string); l++)
  74.    {
  75.       if(string[l]=='\0')
  76.          break;
  77.       result[i]=string[l];
  78.       i++;
  79.    }
  80.    result[i]='\0';
  81.  
  82.    strcpy(ct_buff,result);
  83.    return ct_buff;
  84. }
  85.  
  86. char *left(string,n)
  87. char string[];
  88. int n;
  89. {
  90.    register int l,i;
  91.    char result[MAX_CLMS];
  92.  
  93.    i=0;
  94.  
  95.    for (l=0; l<=n-1; l++)
  96.    {
  97.       if(string[l]=='\0')
  98.          break;
  99.       result[i]=string[l];
  100.       i++;
  101.    }
  102.    result[i]='\0';
  103.  
  104.    strcpy(ct_buff,result);
  105.    return ct_buff;
  106. }
  107.  
  108. char *mid(string,start,n)
  109. char string[];
  110. int start,n;
  111. {
  112.    register int l,i;
  113.    char result[MAX_CLMS];
  114.  
  115.    i=0;
  116.  
  117.    if (start>0 AND n>0)
  118.    {
  119.       for (l=start-1; l<start+n-1; l++)
  120.       {
  121.          if(string[l]=='\0')
  122.             break;
  123.          result[i]=string[l];
  124.          i++;
  125.       }
  126.    }
  127.    result[i]='\0';
  128.  
  129.    strcpy(ct_buff,result);
  130.    return ct_buff;
  131. }
  132.  
  133. char *replicate(chr,n)
  134. char chr;
  135. int n;
  136. {
  137.    char temp[MAX_CLMS];
  138.    int i;
  139.  
  140.    for(i=0; i<n; i++)
  141.       temp[i]=chr;
  142.  
  143.    temp[i]='\0';
  144.  
  145.    if(strlen(temp)>0)
  146.       strcpy(ct_buff,temp);
  147.    else
  148.       strcpy(ct_buff,"");
  149.    return ct_buff;
  150. }
  151.  
  152. char *rjustify(s,n)
  153. char *s;
  154. int n;
  155. {
  156.    char temp[MAX_CLMS];
  157.    int i;
  158.  
  159.    for(i=0; i<n-strlen(s); i++)
  160.       temp[i]=' ';
  161.  
  162.    temp[i] ='\0';
  163.  
  164.    strcat(temp,s);
  165.  
  166.    strcpy(ct_buff,temp);
  167.  
  168.    return ct_buff;
  169. }
  170.  
  171. char *month_str(date)
  172. char date[];
  173. {
  174.    int mn;
  175.    static char *m_name[]={
  176.                            "Invalid\0",
  177.                            "January\0",
  178.                            "February\0",
  179.                            "March\0",
  180.                            "April\0",
  181.                            "May\0",
  182.                            "June\0",
  183.                            "July\0",
  184.                            "August\0",
  185.                            "September\0",
  186.                            "October\0",
  187.                            "November\0",
  188.                            "December\0" };
  189.  
  190.    mn=atoi(mid(date,5,2));
  191.  
  192.    return((mn<1 OR mn>12) ? m_name[0] : m_name[mn]);
  193. }
  194.  
  195. num_days(month,year)
  196. int month,year;
  197. {
  198.    static int days[13]={ 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  199.  
  200.    if (year % 4 EQ 0)
  201.       days[2]=29;
  202.    else
  203.       days[2]=28;
  204.  
  205.    return(days[month]);
  206. }
  207.  
  208. mday(date)
  209. char date[];
  210. {
  211.    int day;
  212.  
  213.    day=atoi(right(date,2));
  214.  
  215.    return(day);
  216. }
  217.  
  218. year(date)
  219. char date[];
  220. {
  221.    int yr;
  222.  
  223.    yr=atoi(left(date,4));
  224.  
  225.    return(yr);
  226. }
  227.  
  228. month(date)
  229. char date[];
  230. {
  231.    int mn;
  232.  
  233.    mn=atoi(mid(date,5,2));
  234.  
  235.    return(mn);
  236. }
  237.  
  238. wday(date)
  239. char date[];
  240. {
  241.    register i,j,k;
  242.    int yr,mn,md,row,column,day;
  243.    BOOLEAN found=FALSE;    
  244.  
  245.    /* CURRENTLY WORKS DURING 1700-2099 PERIOD */
  246.  
  247.    yr=year(date); mn=month(date); md=mday(date);
  248.  
  249.    if(yr<1700 || yr>2099)
  250.       return(-1);
  251.  
  252.    i=0;
  253.  
  254.    while(i<14 AND !found)
  255.    {
  256.       for(j=0; j<12; j++)
  257.       {
  258.          for(k=0; k<4; k++)
  259.             if(yr EQ find_year[i][j]+28*k)
  260.                found=TRUE;
  261.       }
  262.    i++;
  263.    }
  264.  
  265.    if(yr==1700 || yr==1800 || yr==1900) /* SPECIAL CASES */
  266.    {
  267.       found=TRUE;
  268.       if(yr==1700)
  269.          column=7; /* H */
  270.       if(yr==1800)
  271.          column=0; /* A */
  272.       if(yr==1900)
  273.          column=4; /* E */
  274.    }
  275.    else if(yr==1803 || yr==1910) /* VERY SPECIAL CASES */
  276.       column=8; /* I */
  277.    else if(yr==1804)
  278.       column=11; /* L */
  279.    else if(yr==1805 || yr==1811 || yr==1901 || yr==1907)
  280.       column=5; /* F */
  281.    else
  282.       column=i-1;
  283.  
  284.    row=mn-1;
  285.  
  286.    if(!found)
  287.       return(-1);
  288.  
  289.    day=dayarray[column][row];
  290.  
  291.    for (i=0; i<md-1; i++) 
  292.    {
  293.       day++;
  294.       if (day>7)
  295.          day=1;
  296.    }
  297.    return(day);
  298. }
  299.  
  300. char *wday_str(date)
  301. char date[];
  302. {
  303.    int dayweek;
  304.    static char *day_str[]={
  305.                             "\0",
  306.                             "Sunday\0",
  307.                             "Monday\0",
  308.                             "Tuesday\0",
  309.                             "Wednesday\0",
  310.                             "Thursday\0",
  311.                             "Friday\0",
  312.                             "Saturday\0" };
  313.  
  314.    dayweek=wday(date);
  315.    return((dayweek<1 OR dayweek>7) ? day_str[0] : day_str[dayweek]);
  316. }   
  317.  
  318. long calc_bd(sdate,edate)  /* returns number of days between two dates */
  319. char sdate[],edate[];
  320. {
  321.    register int syr,smn,sdy;
  322.    int eyr,emn,edy;
  323.    register long tot;
  324.  
  325.    syr=year(sdate); smn=month(sdate); sdy=mday(sdate);
  326.    eyr=year(edate); emn=month(edate); edy=mday(edate);
  327.  
  328.    if(smn==emn && syr==eyr)
  329.    {
  330.       tot=edy-sdy;
  331.       return(tot);
  332.    }
  333.  
  334.    tot=0;
  335.  
  336.    for(;;)
  337.    {
  338.       if(syr==eyr-1? syr==eyr-1 : syr==eyr)
  339.          break;
  340.       tot+=days_year(syr);
  341.       syr++;
  342.    }
  343.  
  344.    for(;;)
  345.    {
  346.       tot+=num_days(smn,syr);
  347.       smn++;
  348.        if(smn>12)
  349.        {
  350.           smn=1;
  351.           syr++;
  352.        }
  353.        if(smn>emn && syr>=eyr)
  354.           break;
  355.        if(syr>eyr)
  356.           break;
  357.    }
  358.  
  359.    tot-=num_days(emn,eyr);
  360.    tot+=edy-sdy;
  361.  
  362.    return(tot);
  363. }
  364.  
  365. days_year(year)
  366. int year;
  367. {
  368.    if(year % 4 == 0)
  369.       return(366);
  370.    else
  371.       return(365);
  372. }
  373.  
  374. char *fd_forward(date,days)
  375. char date[];
  376. int days;
  377. {
  378.    register int yr,mn,dy,tot;
  379.    char ptr_date[9];
  380.    char mns[3],dys[3],yrs[5],ret_date[9];
  381.  
  382.    yr=year(date); mn=month(date); dy=mday(date);
  383.  
  384.    tot=0;
  385.  
  386.    if(dy+days>num_days(mn,yr))
  387.    {
  388.       while(days-num_days(mn,yr)>tot-dy)
  389.       {
  390.          tot+=num_days(mn,yr);
  391.          mn++;
  392.          if(mn>12)
  393.          {
  394.             mn=1;
  395.             yr++;
  396.          }
  397.       }
  398.       dy=(dy+days)-tot;
  399.       if(dy<=0)
  400.          dy+=num_days(mn,yr);
  401.    }
  402.    else
  403.       dy+=days;
  404.  
  405.    itoa(mn,mns); itoa(dy,dys); itoa(yr,yrs);
  406.  
  407.    strcpy(ret_date,"");
  408.    strcat(ret_date,yrs);
  409.    if(mn<10)
  410.       strcat(ret_date,"0");
  411.    strcat(ret_date,mns);
  412.    if(dy<10)
  413.       strcat(ret_date,"0");
  414.    strcat(ret_date,dys);
  415.    strcpy(ptr_date,ret_date);
  416.  
  417.    return ptr_date;
  418. }
  419.  
  420. char *fd_backward(date,days)
  421. char date[];
  422. int days;
  423. {
  424.    register int yr,mn,dy,tot;
  425.    char ptr_date[9];
  426.    char mns[3],dys[3],yrs[5],ret_date[9];
  427.  
  428.    yr=year(date); mn=month(date); dy=mday(date);
  429.  
  430.    tot=0;
  431.  
  432.    if(dy-days<=0)
  433.    {
  434.       while(days-dy>=tot)
  435.       {
  436.          if(mn==1)
  437.             tot+=num_days(12,yr);
  438.          else
  439.             tot+=num_days(mn-1,yr);
  440.          mn--;
  441.          if(mn<1)
  442.          {
  443.             mn=12;
  444.             yr--;
  445.          }
  446.       }
  447.       dy=tot-days+dy;
  448.    }
  449.    else
  450.       dy-=days;
  451.  
  452.    itoa(mn,mns); itoa(dy,dys); itoa(yr,yrs);
  453.  
  454.    strcpy(ret_date,"");
  455.    strcat(ret_date,yrs);
  456.    if(mn<10)
  457.       strcat(ret_date,"0");
  458.    strcat(ret_date,mns);
  459.    if(dy<10)
  460.       strcat(ret_date,"0");
  461.    strcat(ret_date,dys);
  462.    strcpy(ptr_date,ret_date);
  463.  
  464.    return ptr_date;
  465. }
  466.  
  467. char *itoa(n,s)
  468. int n;
  469. char s[];
  470. {
  471.    int i, sign;
  472.  
  473.    if ((sign=n) <0)
  474.       n = -n;
  475.    i=0;
  476.    do
  477.    {
  478.       s[i++] = n % 10 + '0';
  479.    } while ((n /= 10) >0);
  480.    if (sign<0)
  481.       s[i++]='-';
  482.    s[i]='\0';
  483.    reverse(s);
  484.    return(s);
  485. }
  486.  
  487. char *ltoa(n,s)
  488. long n;
  489. char s[];
  490. {
  491.    long i,sign;
  492.  
  493.    if ((sign=n) <0)
  494.       n = -n;
  495.    i=0;
  496.    do
  497.    {
  498.       s[i++] = n % 10 + '0';
  499.    } while ((n /= 10) >0);
  500.    if (sign<0)
  501.       s[i++]='-';
  502.    s[i]='\0';
  503.    reverse(s);
  504.    return( s );
  505. }
  506.  
  507. reverse(s)
  508. char s[];
  509. {
  510.    int c, i, j;
  511.  
  512.    for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
  513.         c = s[i];
  514.         s[i] = s[j];
  515.         s[j] = c;
  516.    }
  517. }
  518.  
  519. BOOLEAN
  520. /*int*/
  521. instr(string,substring)
  522. char string[],substring[];
  523. {
  524.    int i,j,k;
  525.  
  526.    if(strlen(substring)==0)
  527.       return(FALSE);
  528.  
  529.    for(i=0; string[i]!='\0'; i++)
  530.       for(j=i,k=0; substring[k]==string[j]; k++,j++)
  531.          if(substring[k+1]=='\0')
  532.             return(TRUE);
  533.  
  534.    return(FALSE);
  535. }
  536.  
  537. chg_chr(s,old,new)
  538. char *s,old,new;
  539. {
  540.    int i;
  541.  
  542.    for(i=0; i<strlen(s); i++)
  543.       if(s[i]==old)
  544.          s[i]=new;
  545. }
  546.  
  547. num_suffix(str)
  548. char *str;
  549. {
  550.    if(strcmp(mid(str,strlen(str)-1,1),"1")!=0)
  551.    {
  552.       if(strcmp(right(str,1),"1")==0)
  553.          strcat(str,"st");
  554.       else if(strcmp(right(str,1),"2")==0)
  555.          strcat(str,"nd");
  556.       else if(strcmp(right(str,1),"3")==0)
  557.          strcat(str,"rd");
  558.       else
  559.          strcat(str,"th");
  560.    }
  561.    else
  562.          strcat(str,"th");
  563. }
  564.  
  565. ltrim(str)
  566. char *str;
  567. {
  568.    int i;
  569.  
  570.    for(i=0; str[i]==' '; i++);
  571.  
  572.    strcpy(str,right(str,strlen(str)-i));
  573. }
  574.  
  575. rtrim(str)
  576. char *str;
  577. {
  578.    int i,end;
  579.  
  580.    end=strlen(str);
  581.  
  582.    for(i=end-1; str[i]==' '; i--)
  583.       str[i]='\0';
  584. }
  585.  
  586. upper(str)
  587. char *str;
  588. {
  589.    int i;
  590.  
  591.    for(i=0; str[i]!='\0'; i++)
  592.       str[i]=toupper(str[i]);
  593. }
  594.  
  595. BOOLEAN
  596. word_end(chr)
  597. char chr;
  598. {
  599.    if(chr==' ' || chr=='/' || chr=='.' ||
  600.       chr=='-' || chr==';' || chr==':' ||
  601.       chr=='|' || chr==',' || chr=='\\'||
  602.       chr==']' || chr=='\''|| chr=='\"'||
  603.       chr=='}' || chr==')')
  604.       return(TRUE);
  605.    else
  606.       return(FALSE);
  607. }
  608.  
  609. int
  610. hours_between(start,end)
  611. char start[6],end[6];
  612. {
  613.    int shour,ehour;
  614.  
  615.    shour=atoi(mid(start,2,2));
  616.    if(start[0]=='p')
  617.       shour+=12;
  618.  
  619.    ehour=atoi(mid(end,2,2));
  620.    if(end[0]=='p')
  621.       ehour+=12;
  622.  
  623.    return(ehour-shour);
  624. }
  625.  
  626. int
  627. mins_between(start,end)
  628. char start[6],end[6];
  629. {
  630.    int smin,emin;
  631.  
  632.    smin=atoi(right(start,2));
  633.  
  634.    emin=atoi(right(end,2));
  635.  
  636.    return(emin-smin);
  637. }
  638.  
  639. /*****************************************************************************
  640.  *  File:  misc.c      Miscellaneous modules needed for pingky & Display.      *
  641.  *                                                                           *
  642.  *  Version 1.0       Author: WB Norton  Merit Computer Network              *
  643.  *  Modification History:                                                    *
  644.  *  written 5/18/89   Bill Norton, Merit Computer Network                    *
  645.  *                    Much of this code actually came from the Rochkind      *
  646.  *                    "Advanced UNIX Programming" Book.                      *
  647.  *  09/12/89 - Added itoa from K + R                         *
  648.  *****************************************************************************/
  649. #include <stdio.h>
  650. #include <errno.h>
  651. #include <fcntl.h>
  652. #include <time.h>
  653. #include <sys/types.h>
  654. #include <sys/stat.h>
  655.  
  656. #define LOCKDIR "/tmp/"
  657.  
  658.  
  659. static int logfilevirgin=1;
  660. static char StaticLogFile[100];    /* For routines without access to LogFileName*/
  661. /************************************************************************
  662.  *       Log:         Append a time-stamped log record to a file    *
  663.  ************************************************************************/
  664. void Log(line,logfile)
  665. char *line;             /* line to be logged */
  666. char *logfile;        /* File to log it in */
  667. {
  668. int fd,length;
  669. char Time[27];
  670. char RealFileName[200];
  671. struct tm *tm;
  672. long TimeNow;
  673.  
  674.    if ( logfilevirgin ) {
  675.     strcpy(StaticLogFile,logfile);
  676.     logfilevirgin=0;
  677.    }
  678.    if ( logfile == NULL ) logfile=StaticLogFile;
  679.  
  680.    time( &TimeNow );
  681.    tm = gmtime( &TimeNow );
  682.    sprintf(RealFileName,"%s.%2.2d%2.2d%2.2d",
  683.     logfile,tm->tm_year%100,tm->tm_mon+1,tm->tm_mday);
  684.  
  685.    strcpy(Time,asctime(tm)); Time[strlen(Time)-1]=' '; /* NUKE /n */
  686.  
  687.    length=strlen(line);
  688.  
  689.    if ((fd = open(RealFileName,O_CREAT | O_WRONLY | O_APPEND, 0644)) == -1)
  690.       syserr("Log: Open Log");
  691.    if ( write(fd,Time,strlen(Time)) != strlen(Time)) /*Write Date/Time to disk*/
  692.       syserr("Log: Write Log");
  693.    if ( write(fd,line,length) != length ) /* Write line to disk */
  694.       syserr("Write Log");
  695.    if (close(fd)==-1) syserr("fclose log");
  696. }
  697.  
  698.  
  699. /*****************************************************************************
  700.  *    GetTime - Returns a string in the form we most often want to deal with *
  701.  *                    Mon Mar 23 13:23\0                                     *
  702.  *****************************************************************************/
  703. char *GetTime(putithere)
  704. char *putithere;
  705. {
  706. long t;           /* Structure to hold the time */
  707. struct tm *tm;
  708.  
  709.    time(&t);
  710.    tm=gmtime(&t);
  711.    sprintf(putithere,"%s",asctime(tm));
  712.    putithere[16]=' ';
  713.    putithere[17]='\0';
  714.    return(putithere);
  715. }
  716.  
  717. /****************************************************************
  718.  * FileChanged: Return whether or not the file has changed      *
  719.  ****************************************************************/
  720. BOOLEAN FileChanged(filename,lastmodtime)
  721. char *filename;         /* Filename of file we are interested in     */
  722. long *lastmodtime;      /* Last Modification Time returned from fstat */
  723. {
  724. struct stat buf;
  725.  
  726.    if(stat(filename,&buf) != 0) {
  727.     Empty_File( filename );
  728.     return(TRUE);    /* FILE IS NEW !!! It has changed */
  729.    }
  730.    if ( *lastmodtime != buf.st_mtime) {
  731.       *lastmodtime=buf.st_mtime;
  732.       return(TRUE);    /* File HAS changed */
  733.    }
  734.    return(FALSE);    /* File HAS NOT changed */
  735. }
  736.  
  737. /****************************************************************
  738.  * LastMod:    Return 0 if File Doesn't exist or Mod Time    *
  739.  ****************************************************************/
  740. long LastMod(filename)
  741. char *filename;         /* Filename of file we are interested in     */
  742. {
  743. struct stat buf;
  744.  
  745.    if ( stat(filename,&buf) != 0 ) 
  746.     return( 0 );
  747.    else return(buf.st_mtime);
  748. }
  749.  
  750.  
  751. /****************************************************************
  752.  *        Empty_File:   Empty a file                            *
  753.  ****************************************************************/
  754. void Empty_File(File)
  755. char *File;
  756. {
  757. int pfd;
  758. char buffer[100];
  759.  
  760.     if ((pfd=open(File,O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
  761.     sprintf(buffer,"Empty_File: open %s failed",File);
  762.         syserr(buffer);
  763.     }
  764.     if (close(pfd) == -1)
  765.         syserr("Empty_File: close");
  766. }
  767.  
  768. void panic(str)
  769. char *str;
  770. {
  771. char buffer[200];
  772.   sprintf(buffer,"panic(): ERROR: %s\n",str);
  773.   Log( buffer, NULL );
  774.   fprintf(stderr,buffer);
  775.   perror(str);
  776.   exit(1);
  777. }
  778.  
  779. void syserr(msg) /* print system call error message and terminate */
  780. char *msg;
  781. {
  782.    extern int errno, sys_nerr;
  783.    extern char *sys_errlist[];
  784.  
  785. char buffer[100];
  786.  
  787.    fprintf(stderr,"syserr: %s (%d", msg, errno);
  788.    sprintf(buffer,"syserr: %s (%d; %s)\n", msg, errno,sys_errlist[errno]);
  789.    Log( buffer, NULL );
  790.    if (errno > 0 && errno < sys_nerr) 
  791.       fprintf(stderr,"; %s)\n",sys_errlist[errno]);
  792.    else
  793.       fprintf(stderr,")\n");
  794.    signal(SIGALRM,SIG_DFL);
  795.    signal(SIGHUP,SIG_DFL);
  796.    signal(SIGINT,SIG_DFL);
  797.    signal(SIGQUIT,SIG_DFL);
  798.    signal(SIGTERM,SIG_DFL);
  799.    signal(SIGTSTP,SIG_DFL);
  800.    exit(1);
  801. }
  802.  
  803. void fatal(msg) /* print error message and terminate */
  804. char *msg;
  805. {
  806.    fprintf(stderr,"ERROR: %s\n",msg);
  807.    exit(1);
  808. }
  809.  
  810. #define MAXTRIES 10
  811. char lock(name)    /* Acquire lock */
  812. char *name;    /* File name */
  813. {
  814.    char *path, *lockpath();
  815.    int fd,tries;
  816.    extern int errno;
  817.    static int recursion=0;
  818.    struct stat st;
  819.    char buffer[100];
  820.  
  821.    path=lockpath(name);
  822.    tries=0;
  823.    while((fd=open(path,O_WRONLY|O_CREAT|O_EXCL,0666))==-1 && errno==EEXIST) {
  824.       if (++tries >= MAXTRIES) break;
  825.       sleep(10+(2*tries));
  826.    }
  827.    if (tries >= MAXTRIES) {     /* See if someone stuck th e lock */
  828.     sprintf(buffer,"pid %d: Error...File Locked - Checking how long it was locked\n",getpid());
  829.     Log( buffer , NULL );
  830.     printf(buffer);
  831.     if ( stat( path , &st ) == -1 ) {
  832.         sprintf(buffer,"lock: Lock file didn't exist after 3 tries - lets try again\n");
  833.         printf(buffer);
  834.         return(lock(name));    
  835.     }
  836.     if ( time(0) - st.st_ctime > 60 ) {
  837.         sprintf(buffer,"Lock file LOCKED for more than 60 seconds....I'm unlocking the lock");
  838.         Log( buffer, NULL );
  839.         fprintf(stderr,buffer);
  840.         if ( unlink( path ) == -1 )
  841.             syserr("Unable to unlink LOCK FILE");
  842.         if ( !recursion ) {
  843.             recursion++;
  844.             if (lock(name)) {recursion=0;return(TRUE);}
  845.             else return(FALSE);
  846.         }
  847.         else {
  848.             sprintf(buffer,"LOCK FILE LOCKED, I FREED IT, BUT STILL COULDN'tLOCK THE FILE!!Giving up\n");
  849.             Log( buffer, NULL );
  850.             syserr(buffer);
  851.         }
  852.     }
  853.     else return(FALSE);
  854.    }
  855.    if (fd==-1 || close(fd)==-1 )
  856.     syserr("lock: ");
  857.    return(TRUE);
  858. }
  859.  
  860. void unlock(name)  /* free lock */
  861. char *name;
  862. {
  863.    if (unlink(lockpath(name))==-1) {
  864.       /*(syserr("unlock");*/
  865.     Log("Error: LOCK - someone unlocked the lockfile on me!\n",NULL);
  866.    }
  867. }
  868.  
  869. char *lockpath(name) /* generate lock file path */
  870. char *name;
  871. {
  872.    static char path[100];
  873.    char *strcat();
  874.  
  875.    strcpy(path,name);
  876.    return(strcat(path,".LOCK"));
  877. }
  878.  
  879. /***********************************************************************
  880.  *   LogChecking : Print to a file what node we are currently checking *
  881.  ***********************************************************************/
  882. void LogChecking(str,filename)
  883. char *str,*filename;   /* node name and file name to log checking to */
  884. {
  885. FILE *stream;
  886. char MyTime[40]; /* Read Buffer */
  887.  
  888.    if ((stream=fopen(filename,"w")) == NULL)
  889.       syserr("LogChecking: fopen checking file");
  890.    fprintf(stream,"%s %s",GetTime(MyTime),str);
  891.    if (fclose(stream) == -1) syserr("LogChecking: fclose checking");
  892. }
  893.  
  894. char *_Day( t ) 
  895. long t;
  896. {
  897. struct tm *tm=localtime( &t );
  898.  
  899.     switch( tm->tm_wday ) {
  900.         case 0:    return("Sun");
  901.         case 1:    return("Mon");
  902.         case 2:    return("Tue");
  903.         case 3:    return("Wed");
  904.         case 4:    return("Thu");
  905.         case 5:    return("Fri");
  906.         case 6:    return("Sat");
  907.         default: return("DAY");
  908.     }
  909. }
  910.  
  911. char *_Time( t )
  912. long t;
  913. {
  914. struct tm *tm=localtime( &t );
  915. static char Time[10];
  916.     
  917.     sprintf(Time,"%02.2d:%02.2d",tm->tm_hour,tm->tm_min);
  918.     return( Time );
  919. }
  920.